package com.example.flutter_app_echarts;

/**
 * 参考: Flutter 升级 1.12 导致问题
 * https://github.com/flutter/flutter/issues/61292
 * https://flutter.dev/docs/development/platform-integration/platform-channels
 */

import android.os.Handler;

import androidx.annotation.NonNull;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

import java.io.BufferedReader;
import java.io.IOException;


public class MainActivity extends FlutterActivity {
    private final String CHANNEL = "connect.flutter";
    int count = 0;

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);

        EventChannel messageChannel = new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "flutter_plugin_send");

        messageChannel.setStreamHandler(new EventChannel.StreamHandler() {
            private BluetoothUtils utils = new BluetoothUtils();

            @Override
            public void onListen(Object o, EventChannel.EventSink eventSink) {
                try {
                    BufferedReader CodeReader = this.utils.inputStreamNotify(MainActivity.this);
                    eventSink.success("mo");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancel(Object o) {
            }
        });


        MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL);
        methodChannel.setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    private BluetoothUtils utils = new BluetoothUtils();

                    @Override
                    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                        String name_method = methodCall.method;
                        switch (name_method) {
                            case "connectState":
                                String address = methodCall.argument("address");
                                try {
                                    String code = this.utils.connectState(address);
                                    result.success(code);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    result.error("UNAVAILABLE", "连接失败", null);
                                }
                                break;
                            case "inputStreamNotify":
                                try {
                                    System.out.println(this.utils.mSocket);
                                    BufferedReader CodeReader = this.utils.inputStreamNotify(MainActivity.this);
                                    if (CodeReader != null) {
                                        String message = CodeReader.readLine();
                                        while (message != null) {
                                            message = CodeReader.readLine();
                                            System.out.println(message);
                                            result.success(message);
                                        }
                                    } else {
                                        result.success("404");
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    result.error("UNAVAILABLE", "获取数据失败", null);
                                }
                                break;
                            case "idSearch":
                                this.utils.registerBlueToothConnectReceiver(MainActivity.this);
                                Boolean isEnabled = this.utils.isEnabled();
                                result.success(isEnabled ? "idSearch" : "isEnabled");
                                break;
                            default:
                                result.notImplemented();
                        }
                    }
                }
        );
    }
}
